/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package introse;

import com.sun.net.ssl.internal.ssl.Debug;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author Rommel
 */
public class transactionViewer extends javax.swing.JPanel {

    private transactionManager transactionMngr;
    private uneditableTableModel transactionTableModel;

    public transactionViewer() {
        initComponents();
    }
    
    public final void loadAllTransactionTable() {
        transactionMngr = new transactionManager();
        transactionTableModel = new uneditableTableModel(new String[]{"Transaction Number", "Invoice No.", "Date and Time", "Client", "Amount", "Remark"});
        transactionTable.setModel(transactionTableModel);

        transactionTable.removeColumn(transactionTable.getColumnModel().getColumn(0));

        for (transaction trans : transactionMngr.getTransactions()) {
            String transactionDate = new SimpleDateFormat("EEE, MMM d, yyyy, h:mm a").format(trans.getDateAndTime());
            transactionTableModel.addRow(new String[]{Integer.toString(trans.getTransactionNumber()), trans.getInvoiceNumber(), transactionDate.toString(), trans.getClientName(), Float.toString(trans.getTotalAmount()) + " PHP", trans.getRemark()});
        }
    }

    public final void loadTransactionTable() {
        dateChooser.setDate(new GregorianCalendar().getTime());
        loadTransactionTable(dateChooser.getDate());
    }

    public final void loadTransactionTable(Date selectedDate) {
        transactionMngr = new transactionManager();
        transactionTableModel = new uneditableTableModel(new String[]{"Transaction Number", "Invoice No.", "Date and Time", "Client", "Amount", "Remark"});
        transactionTable.setModel(transactionTableModel);

        transactionTable.removeColumn(transactionTable.getColumnModel().getColumn(0));

        for (transaction trans : transactionMngr.getTransactionsAtDate(selectedDate)) {
            String transactionDate = new SimpleDateFormat("EEE, MMM d, yyyy, h:mm a").format(trans.getDateAndTime());
            transactionTableModel.addRow(new String[]{Integer.toString(trans.getTransactionNumber()), trans.getInvoiceNumber(), transactionDate.toString(), trans.getClientName(), Float.toString(trans.getTotalAmount()) + " PHP", trans.getRemark()});
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        viewTransactionButton = new javax.swing.JButton();
        addTransactionButton = new javax.swing.JButton();
        deleteTransactionButton = new javax.swing.JButton();
        viewDailySalesReportButton = new javax.swing.JButton();
        transactionTableScrollPanel = new javax.swing.JScrollPane();
        transactionTable = new javax.swing.JTable();
        dateChooser = new com.toedter.calendar.JDateChooser();
        goButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(1000, 600));

        viewTransactionButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Transaction View More.png"))); // NOI18N
        viewTransactionButton.setText("<html>\n<p align=\"center\">\n\nView <br> \nTransaction\n\n</p>\n</html>");
        viewTransactionButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        viewTransactionButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        viewTransactionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewTransactionButtonActionPerformed(evt);
            }
        });

        addTransactionButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Transaction Add.png"))); // NOI18N
        addTransactionButton.setText("Add Transaction");
        addTransactionButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addTransactionButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        addTransactionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTransactionButtonActionPerformed(evt);
            }
        });

        deleteTransactionButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Transaction Minus.png"))); // NOI18N
        deleteTransactionButton.setText("<html>\n<p align=\"center\">\n\nDelete <br> \nTransaction\n\n</p>\n</html>");
        deleteTransactionButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteTransactionButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        deleteTransactionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteTransactionButtonActionPerformed(evt);
            }
        });

        viewDailySalesReportButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Generate Report.png"))); // NOI18N
        viewDailySalesReportButton.setText("<html> <p align=\"center\">  View Daily <br>  Sales Report  </p> </html>");
        viewDailySalesReportButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        viewDailySalesReportButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        viewDailySalesReportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewDailySalesReportButtonActionPerformed(evt);
            }
        });

        transactionTable.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        transactionTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Invoice No.", "Date and Time", "Client", "Amount", "Remark"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        transactionTableScrollPanel.setViewportView(transactionTable);

        goButton.setText("Go!");
        goButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goButtonActionPerformed(evt);
            }
        });

        jButton1.setText("View All Transactions");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Date:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(transactionTableScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 861, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(goButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(viewTransactionButton)
                    .addComponent(deleteTransactionButton)
                    .addComponent(addTransactionButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(viewDailySalesReportButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(goButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(dateChooser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(viewTransactionButton, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addTransactionButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteTransactionButton, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(viewDailySalesReportButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(transactionTableScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addTransactionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTransactionButtonActionPerformed
        new addTransaction(null, true).setVisible(true);
        loadTransactionTable();
    }//GEN-LAST:event_addTransactionButtonActionPerformed

    private void viewTransactionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewTransactionButtonActionPerformed
        try {
            int transactionNumber = Integer.parseInt(transactionTableModel.getValueAt(transactionTable.getSelectedRow(), 0).toString());
            viewTransaction transView = new viewTransaction(null, true);
            transView.setTransaction(transactionMngr.getTransaction(transactionNumber));
            transView.setVisible(true);
        } catch (Exception ex) {
            errorDialogue errorMessage = new errorDialogue(null, true);
            errorMessage.setMessage("Please select a transaction");
            errorMessage.setVisible(true);
        }
    }//GEN-LAST:event_viewTransactionButtonActionPerformed

    private void deleteTransactionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteTransactionButtonActionPerformed
        transaction selectedTransaction;
        try {
            selectedTransaction = transactionMngr.getTransaction(Integer.parseInt(transactionTableModel.getValueAt(transactionTable.getSelectedRow(), 0).toString()));
        } catch (Exception ex) {
            errorDialogue errorMessage = new errorDialogue(null, true);
            errorMessage.setMessage("Please select a transaction");
            errorMessage.setVisible(true);
            return;
        }

        deactivationDialog deleteDialog = new deactivationDialog(null, true);
        deleteDialog.setMessage("Are you sure you want to delete?");
        deleteDialog.setVisible(true);

        if (deleteDialog.getReturnStatus() == 1) {
            try {
                selectedTransaction.deleteTransaction();
                positiveMessage posMsg = new positiveMessage(null, true);
                posMsg.setMessage("The transaction has been deleted");
                posMsg.setVisible(true);
            } catch (Exception ex) {
                Debug.println("", ex.toString());
            }
            loadTransactionTable();
        }
    }//GEN-LAST:event_deleteTransactionButtonActionPerformed

    private void viewDailySalesReportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewDailySalesReportButtonActionPerformed
        new dailySalesReport(null, true).setVisible(true);
    }//GEN-LAST:event_viewDailySalesReportButtonActionPerformed

    private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goButtonActionPerformed
        loadTransactionTable(dateChooser.getDate());
    }//GEN-LAST:event_goButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        loadAllTransactionTable();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addTransactionButton;
    private com.toedter.calendar.JDateChooser dateChooser;
    private javax.swing.JButton deleteTransactionButton;
    private javax.swing.JButton goButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTable transactionTable;
    private javax.swing.JScrollPane transactionTableScrollPanel;
    private javax.swing.JButton viewDailySalesReportButton;
    private javax.swing.JButton viewTransactionButton;
    // End of variables declaration//GEN-END:variables
}
